# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/8/17 9:43
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : ui_struct_bak.py
# @Version: OpenTSN_planner_builder 1.0


from PySide6 import QtCore
from PySide6.QtCore import QSize, QPoint
from PySide6.QtGui import QIcon, QMouseEvent
from PySide6.QtWidgets import QPushButton, QMainWindow, QWidget, QLabel


class TSNNode:
    def __init__(self,
                 node_id,
                 node_type):
        # 当前节点id
        self.node_id = node_id
        self.node_type = node_type
        self.node_name = '%s%s' % (node_type, node_id)
        # 孩子节点
        self.children = []
        self.button = None

    def appendChildren(self,
                       node):
        self.children.append(node)


class DraggableButton(QPushButton):
    def __init__(self, title, parent,
                 label):
        super().__init__(title, parent)
        self.iniDragCor = [0, 0]
        self.label = label
        # print(self.hasMouseTracking())

    def mousePressEvent(self, e):
        self.iniDragCor[0] = e.position().x()
        self.iniDragCor[1] = e.position().y()

    def mouseMoveEvent(self, e: QMouseEvent):
        if e.buttons() == QtCore.Qt.LeftButton:
            # print(id(self))
            x = e.position().x() - self.iniDragCor[0]
            y = e.position().y() - self.iniDragCor[1]

            cor = QPoint(x, y)
            label_cor = QPoint(x, y + 35)
            self.move(self.mapToParent(cor))  # 需要apparent一下才可以的,否则只是相对位置。
            self.label.move(self.mapToParent(label_cor))
            # 获取当前button的儿子button


class TSNNodeButton:
    def __init__(self,
                 node: TSNNode,
                 widget: QWidget):
        self.tsn_node = node

        self.label = QLabel(node.node_name, widget)
        self.label.setMaximumSize(35, 20)
        self.label.setMaximumSize(35, 20)

        self.drag_button = DraggableButton(node.node_id, widget, self.label)
        self.drag_button.setMaximumSize(35, 35)
        self.drag_button.setMaximumSize(35, 35)

        icon = QIcon()
        if node.node_type == 'SW':
            icon.addFile('../img/sw_mini.png')
        elif node.node_type == 'ES':
            icon.addFile('../img/es_mini.png')

        self.drag_button.setIcon(icon)
        self.drag_button.setIconSize(QSize(35, 35))
        self.drag_button.setStyleSheet("QPushButton{background:rgba(0,0,0,0);border:1px solid rgba(0,0,0,0);}"
                                       "QPushButton:hover{background-color: rgb(119,136,153);border:none;color:rgb("
                                       "255, "
                                       "255, "
                                       "255);} "
                                       "QPushButton:checked{background-color: rgb(119,136,153);border:none;color:rgb("
                                       "255, "
                                       "255, "
                                       "255);}")

        self.pos_x = 0
        self.pos_y = 0
        self.label_pos_x = 0
        self.label_pos_y = 0
        self.children = []

    def setPos(self, x, y):
        self.pos_x = x
        self.pos_y = y
        self.label_pos_x = self.pos_x
        self.label_pos_y = self.pos_y + 35
        self.drag_button.iniDragCor = [x, y]
        self.drag_button.move(self.pos_x, self.pos_y)
        self.label.move(self.label_pos_x, self.label_pos_y)

    def appendChildren(self,
                       children_button):
        self.children.append(children_button)
